<template>
  <div class="shoppingCar">
    <div class="top">
      <span>全部</span>
      <span>商品</span>
      <span>单价(元)</span>
      <span>数量</span>
      <span>总计(元)</span>
      <span>操作</span>
    </div>
    <ul class="middle">
      <li v-for="(item) in ShoppingCar.data" :key="item.id">
        <el-checkbox v-model="item.isSelect" ></el-checkbox>
        <div class="goods">
          <img :src="item.goodsImg" alt="" />
          <span>
            {{item.goodsTitle}}
          </span>
        </div>
        <span>￥{{item.price}}</span>
        <el-input-number
          v-model="item.num"
          :min="1"
          :max="10"
          @click.native="changNum(item)"
        ></el-input-number>
        <span style="color:#c81623;font-weight:bold">￥{{item.TitlePrice}}</span>
        <div class="operate">
          <el-button type="primary" size="default"  @click='removeGoods(item.id)'>删除</el-button>
        </div>
      </li>
    </ul>
    <div class="footer">
      <el-checkbox v-model="isAllTrue" @change.native='changeAllChoose'>全选</el-checkbox>
      <a href="javascript:;">删除选中的商品</a>
      <a href="javascript:;">已选择 <span>{{goodsNum}}</span> 件商品</a>
      <a href="javascript:;">总价（不含运费）：<span style="color:#c81623;font-weight:bold">￥{{titleprice}}</span></a>
      <el-button type="danger" size="default" @click='goPay'>结算</el-button>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  name: 'shoppingCar',
  data () {
    return {
      goodsNum: 0,
      titleprice: 0,
      isAllTrue: false
    }
  },
  watch: {
    ShoppingCar: {
      deep: true,
      handler (newval, oldval) {
        var count = 0
        var titleprice = 0
        var goodsCount = 0
        newval.data.forEach(element => {
          this.$store.dispatch('shoppingCarAbout/updateGoods', element)
          if (element.isSelect === true) {
            goodsCount += element.num
            count++
            titleprice += parseFloat(element.TitlePrice)
          }
        })
        this.goodsNum = goodsCount
        this.titleprice = titleprice
        if (newval.data.length === count) {
          this.isAllTrue = true
        } else {
          this.isAllTrue = false
        }
      }
    }
  },
  methods: {
    // 获取用户名
    changeAllChoose () {
      if (this.isAllTrue === true) {
        this.ShoppingCar.data.forEach((ele) => {
          ele.isSelect = true
        })
      } else {
        this.ShoppingCar.data.forEach((ele) => {
          ele.isSelect = false
        })
      }
    },
    goPay () {
      this.$router.push({ name: 'pay' })
    },
    getCar () {
      this.$store.dispatch('shoppingCarAbout/getShopCar')
    },
    changNum (item) {
      item.TitlePrice = item.price * item.num
      this.$store.dispatch('shoppingCarAbout/updateGoods', item)
    },
    removeGoods (id) {
      this.$store.dispatch('shoppingCarAbout/removeGoods', id).then((result) => {
        this.getCar()
        if (this.$store.state.shoppingCarAbout.removeGoods.status === 0) {
          this.$success(this.$store.state.shoppingCarAbout.removeGoods.msg)
        } else {
          this.$success(this.$store.state.shoppingCarAbout.removeGoods.msg)
        }
      })
    }
  },
  computed: {
    ...mapState('shoppingCarAbout', ['ShoppingCar'])
  },
  mounted () {
    this.getCar()
    if (this.ShoppingCar.status === 1) {
      this.$error(this.ShoppingCar.msg)
      this.$router.push({ name: 'login' })
    }
  }
}
</script>

<style scoped lang="less">
.shoppingCar {
  width: 1200px;
  margin: 0 auto;
  .top {
    height: 35px;
    background-color: #f5f5f5;
    display: flex;
    font-size: 13px;
    font-weight: bold;
    line-height: 35px;
    justify-content: space-around;
    & > span:nth-child(2) {
      width: 200px;
      text-align: center;
    }
  }
  .middle {
    li {
      margin: 10px 0;
      border: 1px solid #ccc;
      display: flex;
      align-items: center;
      justify-content: space-around;
      color: #333;
      font-size: 14px;
      .goods {
        display: flex;
        align-items: center;
        width: 300px;
        img {
          width: 100px;
          height: 100px;
        }
        span {
          width: 150px;
          font-size: 12px;

        }
      }
      &>span{
        width: 65px;
      }
      &>.el-input-number{
        width: 200px;
      }
    }
  }
  .footer {
    height: 50px;
    border: 1px solid #ccc;
    display: flex;
    justify-content: space-around;
    font-size: 14px;
    color: #999;
    align-items: center;
    a {
      span {
        color: #c81623;
      }
    }
    .el-button {
      width: 100px;
      height: 50px;
    }
  }
}
</style>
